package com.bobo.service.topic;


import com.bobo.dao.topic.ITopicDao;
import com.bobo.dao.topic.TopicDaoImpl;

import com.bobo.dto.Page;
import com.bobo.dto.VCTopic;
import com.bobo.model.Topic;

import java.util.List;

/**
 * Created by bobo on 16-10-28.
 */
public class TopicServiceImpl implements ITopicService {
    ITopicDao topicDao = new TopicDaoImpl();

    @Override
    public Integer create(Topic topic) {
        return topicDao.create(topic);
    }

    @Override
    public boolean delete(Integer id) {
        return topicDao.delete(id);
    }

    @Override
    public boolean update(Topic topic) {
        return topicDao.update(topic);
    }

    @Override
    public Topic getById(Integer id) {
        Topic topic = topicDao.getById(id);
        return topic;
    }

    @Override
    public Page<Topic> getLike(Integer current, Integer pageSize, String like) {
        int count = topicDao.getVCTCount(like);
        Page<Topic> page = (Page<Topic>)setPage(current, pageSize, count);
        page.setList(topicDao.getLike(current,pageSize,like));
        return page;
    }

    @Override
    public Page<VCTopic> getTopicsByUserId(Integer userId, Integer current, Integer pageSize) {

        int count = topicDao.getCount(1,userId,null);
        Page<VCTopic> page = (Page<VCTopic>)setPage(current, pageSize, count);
        page.setList(topicDao.getTus(userId,current,pageSize));
        return page;
    }

    @Override
    public Page<Topic> getTopicsByCategoryId(Integer categoryId, Integer current, Integer pageSize, Integer status) {


        int count = topicDao.getCount(status,null,categoryId);
        Page<Topic> page = (Page<Topic>)setPage(current, pageSize, count);
        page.setList(topicDao.getTopics(null,categoryId,current,pageSize,status));
        return page;
    }

    @Override
    public Page<Topic> getTopics( Integer current, Integer pageSize, Integer status) {

         int count = topicDao.getCount(status,null,null);
        Page<Topic> page = (Page<Topic>)setPage(current, pageSize, count);

        page.setList(topicDao.getTopics(null,null,current,pageSize,status));
        return page;
    }

    public static Page<?>  setPage(Integer current, Integer pageSize, Integer count) {

        Page<?> page = new Page<>();
        if(pageSize < 1) {
            pageSize = 10;
        }
        int countPage = count % pageSize != 0 ? (count / pageSize + 1) : (count / pageSize);

        if(current < 1 ) current = 1;
        if(current > countPage)   current  = countPage;

        page.setPageNum(countPage);
        page.setPageSize(pageSize);
        page.setCurrent(current);

        return page;
    }

    @Override
    public Page<VCTopic> getVCTopics(Integer current, Integer pageSize, String sortName, String sort, String like) {

        int count = topicDao.getVCTCount(like);
        Page<VCTopic> page = (Page<VCTopic>)setPage(current, pageSize, count);
        page.setList(topicDao.getVCTopics(current, pageSize, sortName, sort, like));
        return page;
    }

    @Override
    public List<Topic> getTops(Integer top) {
        return topicDao.getTops(top);
    }


}
